package com.jike.shop.cloud.controller;

import com.google.gson.Gson;
import com.jike.shop.cloud.entity.DemoOrder;
import com.jike.shop.cloud.entity.User;
import com.jike.shop.cloud.mapper.DemoOrderMapper;
import com.jike.shop.cloud.mapper.UserMapper;
import com.jike.shop.cloud.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DemoOrderMapper orderMapper;
    @Autowired
    private DataSourceService dataSourceService;

    @GetMapping("/user")
    public String testUser() {
        User user = userMapper.selectById(1);
        return new Gson().toJson(user);
    }

    @GetMapping("/order")
    public String testOrder() {
        DemoOrder order = orderMapper.selectById(1);
        return new Gson().toJson(order);
    }

    @GetMapping("/db/orders")
    public String orderList() {
        List<DemoOrder> orders = dataSourceService.getOrderInfoAll();
        return new Gson().toJson(orders);
    }

    @GetMapping("/db/order")
    public String getOrderInfoById() {
        DemoOrder order = dataSourceService.getOrderInfoById(1L);
        return new Gson().toJson(order);
    }

    @GetMapping("/db/save")
    public boolean saveOrderInfo() {
        boolean result = dataSourceService.saveOrderInfo(DemoOrder.builder().orderId(123L).userId("zhangsan").price(new BigDecimal("20")).build());
        return result;
    }

    @GetMapping("/db/delete")
    public boolean deleteOrderInfo(long id) {
        boolean result = dataSourceService.deleteOrderInfo(id);
        return result;
    }

}